package com.editors;

/**
 * @author Piazza Francesco Giovanni ,Tecnes Milano http://www.tecnes.com
 *
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.RepaintManager;
import javax.swing.TransferHandler;
import javax.swing.border.Border;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import com.LineData;
import com.Point3D;
import com.Polygon3D;
import com.PolygonMesh;

/**
 * @author Piazza Francesco Giovanni ,Tecnes Milano http://www.tecnes.com
 *
 */

public class ObjectEditorPanel extends JPanel implements ActionListener,KeyListener, ItemListener,MouseListener,MouseWheelListener,MouseMotionListener{


	public ObjectEditor oe=null;
	
	
	public int HEIGHT=550;
	public int WIDTH=600;

	public int FRONT_VIEW_HEIGHT=300;
	public int FRONT_VIEW_WIDTH=300;

	public int TOP_VIEW_HEIGHT=HEIGHT-FRONT_VIEW_HEIGHT;
	public int TOP_VIEW_WIDTH=300;
	
	

	public int RIGHT_BORDER=320;
	public int BOTTOM_BORDER=100;

	Graphics2D g2;


	public JFileChooser fc;

	public JPanel right;
	public JTextField coordinatesx;
	public JTextField coordinatesy;
	public JTextField coordinatesz;

	public JButton addPoint;
	public JButton deleteSelection;
	public JButton changePoint;
	public JButton deselectAll;;
	public JCheckBox checkMultipleSelection;
	public JList pointList=null;


	public JButton joinPoints;
	public JButton buildPolygon;
	

	public boolean ISDEBUG=false;
	public JPanel bottom;
	public JLabel screenPoint;
	public JCheckBox checkCoordinatesx;
	public JCheckBox checkCoordinatesz;
	public JCheckBox checkCoordinatesy;


	public boolean isUseTextures=true;


	public JList lineList;
	public JButton selectAll;
	public LineData polygon=new LineData();


	public double alfa=Math.PI/4;
	public double sinAlfa=Math.sin(alfa);
	public double cosAlfa=Math.cos(alfa);
	public double fi=0;
	public double sinf=Math.sin(fi);
	public double cosf=Math.cos(fi);
	public JButton rescale;
	public DoubleTextField rescaleValue;

	public DoubleTextField objMove;
	public JButton movePointUp;
	public JButton movePointDown;
	public JButton movePointLeft;
	public JButton movePointRight;
	public JButton movePointTop;
	public JButton movePointBottom;
	
	public File currentDirectory=null;

	public DecimalFormat dfc=null;



	public ObjectEditorPanel(ObjectEditor oe){

	
		this.oe=oe;
		
		setLayout(null);
		addKeyListener(this);
		addMouseListener(this);
		addMouseWheelListener(this);
		addMouseMotionListener(this);



	}
	
	/**
	 * 
	 */
	public void initialize() {


		g2=(Graphics2D) getGraphics();
		
		DecimalFormatSymbols dfs=new DecimalFormatSymbols(Locale.UK);
		dfc=new DecimalFormat("###.##");
		dfc.setDecimalFormatSymbols(dfs);
		

	}


	public void displayAll() {
 
		
	}


	public Area clipPolygonToArea2D(Polygon p_in,Area area_out){


		Area area_in = new Area(p_in);

		Area new_area_out = (Area) area_out.clone();
		new_area_out.intersect(area_in);

		return new_area_out;

	}


	public void actionPerformed(ActionEvent arg0) {
		Object o=arg0.getSource();

		if(o==addPoint){ 
			oe.prepareUndo();
			addPoint();
		}	
		else if(o==deleteSelection) {
			oe.prepareUndo();
			delete();
		}	
		else if(o==changePoint){
			oe.prepareUndo();
			changeSelectedPoint();
			displayAll();
		}
		else if(o==joinPoints){
			oe.prepareUndo();
			joinSelectedPoints();
			displayAll();
		}
		else if(o==buildPolygon){
			oe.prepareUndo();
			buildPolygon();
			displayAll();
		}
		else if(o==deselectAll){
			deselectAll();
			displayAll();
		}
		else if(o==selectAll){
			selectAllPoints();
			displayAll();
		}
		else if(o==rescale){
			oe.prepareUndo();
			rescaleAllPoints();
			displayAll();
		}
		else if(o==movePointUp){
			oe.prepareUndo();
			moveSelectedPoints(0,0,1);
			displayAll();
		}
		else if(o==movePointDown){
			oe.prepareUndo();
			moveSelectedPoints(0,0,-1);
			displayAll();
		}
		else if(o==movePointLeft){
			oe.prepareUndo();
			moveSelectedPoints(0,-1,0);
			displayAll();
		}
		else if(o==movePointRight){
			oe.prepareUndo();
			moveSelectedPoints(0,+1,0);
			displayAll();
		}
		else if(o==movePointTop){
			oe.prepareUndo();
			moveSelectedPoints(1,0,0);
			displayAll();
		}
		else if(o==movePointBottom){
			oe.prepareUndo();
			moveSelectedPoints(-1,0,0);
			displayAll();
		}

	}

	
	public void moveSelectedPoints(int dx, int dy, int dz) { 
		
		String sqty=objMove.getText();
		
		if(sqty==null || sqty.equals(""))
			return;
		
		double qty=Double.parseDouble(sqty);
		
		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);

			if(p.isSelected()){

				
					p.x=p.x+qty*dx;
					p.y=p.y+qty*dy;
					p.z=p.z+qty*dz;
					
					coordinatesx.setText(dfc.format(p.x));
					coordinatesy.setText(dfc.format(p.y));
					coordinatesz.setText(dfc.format(p.z));
			}

		}
		//deselectAll();

		
	}


	public void rescaleAllPoints() {
		
		String txt=rescaleValue.getText();
		
		if(txt==null || txt.equals(""))
			return;
		double val=Double.parseDouble(txt);
		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);
		    p.x=Math.round(p.x *val);
		    p.y=Math.round(p.y *val);
		    p.z=Math.round(p.z *val);
		}
		
	}


	
	protected void selectAllPoints() {
		
		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);
			p.setSelected(true);
		}
		
	}

	public void buildPolygon() {


        oe.lines.add(polygon);
        

		deselectAll();

	}


	public void joinSelectedPoints() {
		for(int i=0;i<oe.points.size();i++){

			Point3D p0=(Point3D) oe.points.elementAt(i);

			for(int j=0;j<oe.points.size();j++){

				Point3D p1=(Point3D) oe.points.elementAt(j);

				if(p0.isSelected() && p1.isSelected() && i<j)
				{
					LineData ld=new LineData();
					ld.addIndex(i);
					ld.addIndex(j);
					oe.lines.add(ld);

				}

			}

		}
		deselectAll();

	}


	public void changeSelectedPoint() {

		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);

			if(p.isSelected()){

				if(!"".equals(coordinatesx.getText()))
					p.x=Double.parseDouble(coordinatesx.getText());
				if(!"".equals(coordinatesy.getText()))
					p.y=Double.parseDouble(coordinatesy.getText());
				if(!"".equals(coordinatesz.getText()))
					p.z=Double.parseDouble(coordinatesz.getText());
			}

		}

		deselectAll();

	}


	public void addPoint() {

		if("".equals(coordinatesx.getText()) |
				"".equals(coordinatesy.getText()) |
				"".equals(coordinatesz.getText())
		)
			return;
		double x=Double.parseDouble(coordinatesx.getText());
		double y=Double.parseDouble(coordinatesy.getText());
		double z=Double.parseDouble(coordinatesz.getText());

		
		addPoint(x,y,z);

	}
	
	private void addPoint(double x, double y, double z) {

		if(!"".equals(coordinatesx.getText()))
			x=Double.parseDouble(coordinatesx.getText());
		if(!"".equals(coordinatesy.getText()))
			y=Double.parseDouble(coordinatesy.getText());
		if(!"".equals(coordinatesz.getText()))
			z=Double.parseDouble(coordinatesz.getText());

		Point3D p=new Point3D(x,y,z);

		oe.points.add(p);
        
		deselectAll();
		displayAll();
		
		pointList.ensureIndexIsVisible(pointList.getModel().getSize()-1); 

	}


	public void getTemplate() {
		
		ObjectEditorTemplatePanel oetp=new ObjectEditorTemplatePanel();
		
		PolygonMesh pm=oetp.getTemplate();
		
		if(pm!=null){
			oe.points=new Vector();
			for (int i = 0; i < pm.points.length; i++) {
				oe.points.add(pm.points[i]);
			}
			oe.lines=pm.polygonData;
			displayAll();
		}
		
	}



	public void delete() {

		Vector newPoints=new Vector();
		Vector newLines=new Vector();

		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);
			if(!p.isSelected()) 
				newPoints.add(p);

		}

		for(int i=0;i<oe.lines.size();i++){

			LineData ld=(LineData) oe.lines.elementAt(i);
			if(ld.isSelected())
				continue;
			LineData newLd = new LineData();

			for(int j=0;j<ld.size();j++){

				Point3D p0=(Point3D) oe.points.elementAt(ld.getIndex(j));
				if(!p0.isSelected()) 
					for(int k=0;k<newPoints.size();k++){

						Point3D np=(Point3D) newPoints.elementAt(k);
						if(np.equals(p0))
						{
							newLd.addIndex(k);
							break;
						}
					}	

			}
			if(newLd.size()>1)
				newLines.add(newLd);




		}

		oe.points=newPoints;
		oe.lines=newLines;
        deselectAll();
		

		displayAll();

	}

	public void deselectAll() {
		
		clean();
		deselectAllLines();
		deselectAllPoints();
		polygon=new LineData();

	
		
	
	}

	public void deselectAllPoints() {
		
		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);
			p.setSelected(false);
		}
		
	}
	
	public void deselectAllLines() {
		
		for(int i=0;i<oe.lines.size();i++){

			LineData ld=(LineData) oe.lines.elementAt(i);
			ld.setSelected(false);
		}
		
	}

	public void saveLines() {

		fc = new JFileChooser();
		fc.setDialogType(JFileChooser.SAVE_DIALOG);
		fc.setDialogTitle("Save lines");
		if(currentDirectory!=null)
			fc.setCurrentDirectory(currentDirectory);
		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			currentDirectory=fc.getCurrentDirectory();
			saveLines(file);

		} 


	}

	public void saveLines(File file) {



		PrintWriter pr;
		try {
			pr = new PrintWriter(new FileOutputStream(file));
			pr.print("P=");

			for(int i=0;i<oe.points.size();i++){

				Point3D p=(Point3D) oe.points.elementAt(i);

				pr.print(decomposePoint(p));
				if(i<oe.points.size()-1)
					pr.print("_");
			}	

			pr.print("\nL=");

			for(int i=0;i<oe.lines.size();i++){

				LineData ld=(LineData) oe.lines.elementAt(i);

				pr.print(decomposeLineData(ld));
				if(i<oe.lines.size()-1)
					pr.print("_");
			}	

			pr.close(); 	

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
	}
	
	public void savePolyFormat() {
		
		fc = new JFileChooser();
		fc.setDialogType(JFileChooser.SAVE_DIALOG);
		fc.setDialogTitle("Save other format");
		if(currentDirectory!=null)
			fc.setCurrentDirectory(currentDirectory);
		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			currentDirectory=fc.getCurrentDirectory();
			File file = fc.getSelectedFile();
			savePolyFormat(file);

		} 
		
	}



	public void savePolyFormat(File file) {



		PrintWriter pr;
		try {
			pr = new PrintWriter(new FileOutputStream(file));
		
			for(int i=0;i<oe.lines.size();i++){

				LineData ld=(LineData) oe.lines.elementAt(i);

				pr.println(decomposePolyFormat(ld));
			
			}	

			pr.close(); 	

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
	}


	public void saveBaseCubicTexture() {
		
		fc = new JFileChooser();
		fc.setDialogType(JFileChooser.SAVE_DIALOG);
		fc.setDialogTitle("Save basic texture");
		if(currentDirectory!=null)
			fc.setCurrentDirectory(currentDirectory);
		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			currentDirectory=fc.getCurrentDirectory();
			File file = fc.getSelectedFile();
			saveBaseCubicTexture(file);

		}
		
	}


	public void saveBaseCubicTexture(File file) {
		
		Color backgroundColor=Color.green;
		Color back_color=Color.BLUE;
		Color top_color=Color.RED;
		Color left_color=Color.YELLOW;
		Color right_color=Color.ORANGE;
		Color front_color=Color.CYAN;
		
		int IMG_WIDTH=0;
		int IMG_HEIGHT=0;

		int deltaX=0;
		int deltaY=0;
		int deltaX2=0;

		double minx=0;
		double miny=0;
		double minz=0;
		
		double maxx=0;
		double maxy=0;
		double maxz=0;
		
		
	      //find maxs
		for(int j=0;j<oe.points.size();j++){
			
			Point3D point= (Point3D) oe.points.elementAt(j);
			
			if(j==0){
				
				minx=point.x;
				miny=point.y;
				minz=point.z;
				
				maxx=point.x;
				maxy=point.y;
				maxz=point.z;
			}
			else{
				
				maxx=(int)Math.max(point.x,maxx);
				maxz=(int)Math.max(point.z,maxz);
				maxy=(int)Math.max(point.y,maxy);
				
				
				minx=(int)Math.min(point.x,minx);
				minz=(int)Math.min(point.z,minz);
				miny=(int)Math.min(point.y,miny);
			}
			
	
		}
		
		deltaX2=(int)(maxx-minx);
		deltaX=(int)(maxz-minz);
		deltaY=(int)(maxy-miny);
		
		deltaX2=deltaX2+deltaX;
		
		IMG_HEIGHT=(int) deltaY+deltaX+deltaX;
		IMG_WIDTH=(int) (deltaX2+deltaX);
		
		BufferedImage buf=new BufferedImage(IMG_WIDTH,IMG_HEIGHT,BufferedImage.TYPE_INT_RGB);
	
		try {
			Graphics2D bufGraphics=(Graphics2D)buf.getGraphics();
			
			bufGraphics.setColor(backgroundColor);
			bufGraphics.fillRect(0,0,IMG_WIDTH,IMG_HEIGHT);
			bufGraphics.setColor(front_color);
			bufGraphics.fillRect(deltaX,0,deltaX2-deltaX,deltaX);
			bufGraphics.setColor(top_color);
			bufGraphics.fillRect(deltaX,deltaX,deltaX2-deltaX,deltaY);
			bufGraphics.setColor(left_color);
			bufGraphics.fillRect(0,deltaX,deltaX,deltaY);
			bufGraphics.setColor(right_color);
			bufGraphics.fillRect(deltaX2,deltaX,deltaX,deltaY);
			bufGraphics.setColor(back_color);
			bufGraphics.fillRect(deltaX,deltaY+deltaX,deltaX2-deltaX,deltaX);
			
			ImageIO.write(buf,"jpg",file);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}

	public void buildPoints(Vector points, String str) {

		StringTokenizer sttoken=new StringTokenizer(str,"_");

		while(sttoken.hasMoreElements()){

			String[] vals = sttoken.nextToken().split(",");

			Point3D p=new Point3D();

			p.x=Double.parseDouble(vals[0]);
			p.y=Double.parseDouble(vals[1]);
			p.z=Double.parseDouble(vals[2]);

			points.add(p);
		}




	}

	public void buildLines(Vector lines, String str) {

		StringTokenizer sttoken=new StringTokenizer(str,"_");

		while(sttoken.hasMoreElements()){

			String[] vals = sttoken.nextToken().split(",");

			LineData ld=new LineData();

			for(int i=0;i<vals.length;i++)
				ld.addIndex(Integer.parseInt(vals[i]));


			oe.lines.add(ld);
		}




	}

	public String decomposePoint(Point3D p) {
		String str="";

		str=p.x+","+p.y+","+p.z;

		return str;
	}

	public String decomposeLineData(LineData ld) {

		String str="";

		for(int j=0;j<ld.size();j++){

			if(j>0)
				str+=",";
			str+=ld.getIndex(j);

		}

		return str;
	}
	
	public String decomposePolyFormat(LineData ld) {

		String str="";

		for(int j=0;j<ld.size();j++){

			if(j>0)
				str+="_";
			Point3D p=(Point3D) oe.points.elementAt(ld.getIndex(j));
			str+=p.x+","+p.y+","+p.z;

		}

		return str;
	}


	public void clean(){

		if(!checkCoordinatesx.isSelected())	coordinatesx.setText("");
		if(!checkCoordinatesy.isSelected())coordinatesy.setText("");
		if(!checkCoordinatesz.isSelected())coordinatesz.setText("");
		/*if(!checkCoordinatesdx.isSelected())coordinatesdx.setText("");
		if(!checkCoordinatesdy.isSelected())coordinatesdy.setText("");
		if(!checkCoordinatesdz.isSelected())coordinatesdz.setText("");
		if(!checkObjectIndex.isSelected())objectIndex.setText("");
		if(!checkColor.isSelected())checkColor.setBackground(ZBuffer.fromHexToColor("FFFFFF"));
		 */
		

	}

	public void keyPressed(KeyEvent arg0) {

		int code =arg0.getKeyCode();

		if(code==KeyEvent.VK_DOWN )
			down();
		else if(code==KeyEvent.VK_UP  )
			up();
		else if(code==KeyEvent.VK_LEFT )
		{	left(); 
		   
		}
		else if(code==KeyEvent.VK_RIGHT  )
		{	
			right();   
		
		}
		else if(code==KeyEvent.VK_C  )
		{	
			oe.prepareUndo();
			changeSelectedPoint();   
		    displayAll();   
		}
		else if(code==KeyEvent.VK_J )
		{	
			oe.prepareUndo();
			joinSelectedPoints(); 
			displayAll(); 
		}
		else if(code==KeyEvent.VK_I )
		{	
			oe.prepareUndo();
			addPoint();
		}
		else if(code==KeyEvent.VK_P )
		{	
			oe.prepareUndo();
			buildPolygon(); 
			displayAll(); 
		}
		else if(code==KeyEvent.VK_E )
		{	
			deselectAll();
			displayAll(); 
		}
		else if(code==KeyEvent.VK_S )
		{	
			selectAllPoints();
			displayAll(); 
		}
		else if(code==KeyEvent.VK_D )
		{	
			oe.prepareUndo();
			delete();
			displayAll(); 
		}
		else if(code==KeyEvent.VK_Q )
		{	
			rotate(+0.1);
			displayAll(); 
		}
		else if(code==KeyEvent.VK_W )
		{	
			rotate(-0.1);
			displayAll(); 
		}
		else if(code==KeyEvent.VK_F1 )
		{	
			zoomIn();
			displayAll(); 
		}
		else if(code==KeyEvent.VK_F2 )
		{	
			zoomOut();
			displayAll(); 
		}
		else if(code==KeyEvent.VK_R )
		{	
			rescaleAllPoints(); 
			displayAll(); 
		}
		else if(code==KeyEvent.VK_F3 )
		{	
			checkMultipleSelection.setSelected(true);  
		}
		else if(code==KeyEvent.VK_F4 )
		{	
			 
			checkMultipleSelection.setSelected(false);  
		}
		else if(code==KeyEvent.VK_PAGE_UP )
		{	
			moveSelectedLine(-1); 
		}
		else if(code==KeyEvent.VK_PAGE_DOWN )
		{	
			 
			moveSelectedLine(+1); 
		}
		else if(code==KeyEvent.VK_LESS )
		{	
			 
			invertSelectedLine(); 
		}
		else if(code==KeyEvent.VK_1 )
		{	
			 
			oe.set3DView(); 
		}
		else if(code==KeyEvent.VK_2 )
		{	
			 
			oe.setTopView(); 
		}
		else if(code==KeyEvent.VK_3 )
		{	
			 
			oe.setLeftView(); 
		}
		else if(code==KeyEvent.VK_4)
		{	
			 
			oe.setFrontView(); 
		}

	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void moveSelectedLine(int direction) {
		
		for(int i=0;i<oe.lines.size();i++){

			LineData ld=(LineData) oe.lines.elementAt(i);
			if(ld.isSelected() && direction>0 && i<oe.lines.size()-1 ){
					swapLines(oe.lines,i+1,i);	
					lineList.setSelectedIndex(lineList.getSelectedIndex()+1);
					break;
			}
			else if(ld.isSelected() && direction<0 && i>0){
					swapLines(oe.lines,i,i-1);	
					lineList.setSelectedIndex(lineList.getSelectedIndex()-1);
					break;
			}
		}
		//System.out.println(lineList.getSelectedIndex());
		resetLists();
	}
	
	public void swapLines(Vector lines, int i1, int i2) {
		LineData ld1=(LineData) oe.lines.elementAt(i1);
		LineData ld2=(LineData) oe.lines.elementAt(i2);
		oe.lines.setElementAt(ld1,i2);
		oe.lines.setElementAt(ld2,i1);
	}

	public void invertSelectedLine(){
		
		for(int i=0;i<oe.lines.size();i++){

			LineData ld=(LineData) oe.lines.elementAt(i);
			if(ld.isSelected()){
				
				LineData invertedLd=new LineData();
				
				for (int j = ld.size()-1; j >=0; j--) {
					invertedLd.addIndex(ld.getIndex(j));
				}
				oe.lines.setElementAt(invertedLd,i);
			}
			
		
		}
		resetLists();
		displayAll();
	}
	
	private void resetLists() {
		
		DefaultListModel dlm=new DefaultListModel();
		int sel=pointList.getSelectedIndex();
		

		for(int i=0;i<oe.points.size();i++){

			Point3D p=(Point3D) oe.points.elementAt(i);
		    dlm.addElement(new PointListItem(p,i)) ; 
		}
		
		pointList.setModel(dlm);
		
		if(sel>=0 && sel<pointList.getModel().getSize())
			pointList.setSelectedIndex(sel);
		
		///////////////////////
		
		DefaultListModel dflm=new DefaultListModel();
		int selec=lineList.getSelectedIndex();
		

		for(int i=0;i<oe.lines.size();i++){

			LineData ld=(LineData) oe.lines.elementAt(i);
			dflm.addElement(ld) ; 
		}
		
		lineList.setModel(dflm);
		
		if(selec>=0 && selec<lineList.getModel().getSize())
			lineList.setSelectedIndex(selec);
		
		
	}

	public void mouseClicked(MouseEvent arg0) {

		int buttonNum=arg0.getButton();
		//right button click

		selectPoint(arg0.getX(),arg0.getY());
		displayAll();

	}
	
	public void mouseDragged(MouseEvent arg0) {

	}

	public void mouseMoved(MouseEvent e) {



	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseWheelMoved(MouseWheelEvent arg0) {

		int pix=arg0.getUnitsToScroll();
		if(pix>0) up();
		else down();

	}
	
	public void rotate(double df){
		
		 fi+=df;
		 sinf=Math.sin(fi);
		 cosf=Math.cos(fi);
		
	}

	public void zoomOut() {
		// TODO Auto-generated method stub
		
	}


	public void zoomIn() {
		// TODO Auto-generated method stub
		
	}

	public void right() {
		// TODO Auto-generated method stub
		
	}


	public void left() {
		// TODO Auto-generated method stub
		
	}


	public void up() {
		// TODO Auto-generated method stub
		
	}


	public void down() {
		// TODO Auto-generated method stub
		
	}

	public void moveCenter(int dx,int dy) {
		// TODO Auto-generated method stub
	}


	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void selectPoint(int x, int y){
		// TODO Auto-generated method stub
	}
		
	public class PointListItem{
		
		public DecimalFormat df=new DecimalFormat("000");

		Point3D p;
		int index=-1;

		public PointListItem(Point3D p, int index) {	
			super();
			this.p = p;
			this.index=index;
		}
		
	
		public String toString() {
			
			return df.format(p.x)+","+df.format(p.y)+","+df.format(p.z)+"("+index+")";
		}
		
	}


}
